package com.zqy.controller;

import com.zqy.common.util.BaseDao;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/homePageController")
public class HomePageController {
    static BaseDao bd = new BaseDao();
    static String url = bd.getPropertiesFile("jdbc.properties").getProperty("jdbc.url");
    static String username = bd.getPropertiesFile("jdbc.properties").getProperty("jdbc.username");
    static String password = bd.getPropertiesFile("jdbc.properties").getProperty("jdbc.password");

    /**
     * 输出结果到response中
     *
     * @param response
     * @param str
     */
    private void output(HttpServletResponse response, String str) {
        try {
            response.getOutputStream().write(str.getBytes("UTF-8"));
            response.getOutputStream().flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String UUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid;
    }

    @GetMapping("/test")
    public String test() {
        return "test";

    }

    /**
     * 首页轮播图
     * @param
     * @return
     */
    @RequestMapping("getHomepageimgs")
    private void getHomepageimgs(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        //查询首页轮播图
        String sql = "select t.*,DATE_FORMAT(t.imgdate,'%Y-%m-%d') imgdates from zqy_homepageimgs t ";
        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
        System.out.println("测试git666");
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 新闻图片
     * @param
     * @return
     */
    @RequestMapping("getNewsImgs")
    private void getNewsImgs(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        //查询新闻图片
        String sql = "select * from zqy_newsimgs ";
        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 首页查询公告1
     * @param
     * @return
     */
    @RequestMapping("getNotice")
    private void getNotice(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");

        //查询公告首页列表
        String sql = "select t.noticeid,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes,t.headline,t.classify " +
                "from zqy_notice t " +
                "where   classify='1' ORDER BY t.noticetime desc LIMIT 0,5";

        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 首页查询公告2
     * @param
     * @return
     */
    @RequestMapping("getNotice2")
    private void getNotice2(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");

        //查询公告首页列表
        String sql = "select t.noticeid,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes,t.headline,t.classify " +
                "from zqy_notice t " +
                "where   classify='2' ORDER BY t.noticetime desc  LIMIT 0,3";

        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 首页查询公告3
     * @param
     * @return
     */
    @RequestMapping("getNotice3")
    private void getNotice3(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");

        //查询公告首页列表
        String sql = "select t.noticeid,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes,t.headline,t.classify " +
                "from zqy_notice t " +
                "where   classify='3' ORDER BY t.noticetime desc LIMIT 0,3";

        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 首页查询公告4
     * @param
     * @return
     */
    @RequestMapping("getNotice4")
    private void getNotice4(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");

        //查询公告首页列表
        String sql = "select t.noticeid,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes,t.headline,t.classify " +
                "from zqy_notice t " +
                "where   classify='4' ORDER BY t.noticetime desc LIMIT 0,3";

        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 首页查询公告详情
     * @param
     * @return
     */
    @RequestMapping("getNoticedetail")
    private void getNoticedetail(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        String noticeid=request.getParameter("noticeid");
        //查询公告内容
        String sql = "select t.*,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes from zqy_notice t " +
                "where t.noticeid = '"+noticeid+"' ";

        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
    /**
     * 公告更多列表
     * @param
     * @return
     */
    @RequestMapping("getNoticeList")
    private void getNoticeList(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        int page=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
        int pageCount=request.getParameter("pageCount")!=null?Integer.parseInt(request.getParameter("pageCount")):10;
        String pageQualityDesigner="  LIMIT "+(page-1)*pageCount+","+pageCount;

        //查询公告列表
        String sql = "select t.*,DATE_FORMAT(t.noticetime,'%Y-%m-%d') noticetimes, " +
                "(SELECT COUNT(*)  FROM zqy_notice " +
                "t where t.classify  = '"+id+"') AS counts " +
                "from zqy_notice  t " +
                "where t.classify  = '"+id+"' order by t.noticetime desc " +pageQualityDesigner  ;
//        System.out.println(sql);
        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }

    /**
     * 公告更多列表总数
     * @param
     * @return
     */
    @RequestMapping("getNoticeListCount")
    private void getNoticeListCount(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/text");
        response.setCharacterEncoding("UTF-8");
        String id=request.getParameter("id");
        int page=request.getParameter("page")!=null?Integer.parseInt(request.getParameter("page")):1;
        int pageCount=request.getParameter("pageCount")!=null?Integer.parseInt(request.getParameter("pageCount")):10;
        String pageQualityDesigner="  LIMIT "+(page-1)*pageCount+","+pageCount;

        //查询公告列表
        String sql = "select count(*) counts from zqy_notice  t  " +
                "where t.classify  = '"+id+"' "  ;
        System.out.println(sql);
        List<HashMap> lh = bd.getDateByJDBC(url, username, password, sql);
//        System.out.println(lh);
        JSONArray jsonArray = JSONArray.fromObject(lh);
        output(response, jsonArray.toString());
    }
}
